/*
 左侧功能按钮
 * */

namespace('n2.rightBtn');

n2.rightBtn=function(){
	this._viwer = null;
	this._twoD = false;
	this._camera = null;
	this._isSun = true;
	this._initPosition = [108.400975, 36.903325, 20000000];
	n2.base(this, n2.main);
};


n2.definePrototype(n2.rightBtn.prototype, {
    initPosition: {
        get: function () {
            return this._initPosition;
        }
    },
    viewer: {
        get: function () {
            return this._viewer;
        },
        set: function (value) {
            this._viewer = value;
        }
    },
    twoD:{
         get: function () {
            return this._twoD;
        },
        set: function (value) {
            this._twoD= value;
        }
    },
    camera:{
        get: function () {
            return this._camera;
        },
        set: function (value) {
            this._camera= value;
        }
    }
});


n2.rightBtn.prototype.init=function(){
	var self = this;

    var flag = 0;    //二三维图表切换开关
  
 	self.Btn23D();
//  self.Btnshadow();
  	self.BtnSize();
  	
  	self.double_click();
}


//二三维切换
n2.rightBtn.prototype.Btn23D = function(){
	var self = this;
    var viewer = this.viewer = n2.viewer; 
    var camera = new Cesium.Camera(viewer.scene);  
    n2.dom.byId("btnTDS").addEventListener('tap', function() {
        if (self.twoD) {        	
            viewer.scene.morphTo3D(2.0);
            setTimeout(function () {
                camera.flyTo({
                    destination: new Cesium.Cartesian3.fromDegrees(108.400975, 36.903325, 10000000)  //
                });
            },3000);
            self.twoD = false;
        } else {
            viewer.scene.morphTo2D(2.0);
            setTimeout(function () {
                camera.flyTo({
                    destination: new Cesium.Cartesian3.fromDegrees(108.400975, 36.903325, 10000000)
                });
            },4000);
            self.twoD = true;
        }
    })
}
//
//
////阴影开关
//n2.rightBtn.prototype.Btnshadow = function(){
//	var self = this;
//	var viewer = this.viewer = n2.viewer;   
//  n2.dom.byId("btnSun").onclick = function () {
//      if (self.isSun == true) {
//          viewer.shadows = true;
//          viewer.terrainShadows = true;
//          n2.dom.byId("btnSun").style.backgroundImage = "url(./images/ui/TheSunMaskH.png)";
//          self.isSun = false;
//      } else {
//          viewer.shadows = false;
//          viewer.terrainShadows = false;
//          n2.dom.byId("btnSun").style.backgroundImage = "url(./images/ui/TheSunMask.png)";
//      }
//  }
//}
//
//
//复位
n2.rightBtn.prototype.BtnSize = function(){

	var self = this;
    var viewer = self.viewer=n2.viewer;
    var canvas = viewer.canvas;
    canvas.setAttribute('tabindex', '0');
    canvas.onclick = function () {
        canvas.focus();
    };
    var ellipsoid = viewer.scene.globe.ellipsoid;
    var startMousePosition;
    var mousePosition;

    viewer.clock.onTick.addEventListener(function (clock) {
        var camera = viewer.camera;

        
    
        var larg = n2.dom.byId('larg');
	    var sma = n2.dom.byId('sma');
	    var re = n2.dom.byId('re');
		
//放大
//	    larg.addEventListener('tap', function() {
//	    var cameraHeight = ellipsoid.cartesianToCartographic(camera.position).height;
//	    console.log(cameraHeight)
//      var moveRate = cameraHeight / 10.0;
//	    	camera.moveForward(moveRate);
//	    })    
	    //缩小
//	    sma.addEventListener('tap', function() {
//	var cameraHeight = ellipsoid.cartesianToCartographic(camera.position).height;
//      var moveRate = cameraHeight / 10.0;
//	    	camera.moveBackward(moveRate);
//	
//	    })
	    

	    //复位
	    re.addEventListener('tap', function() {
			
	    	camera.flyTo({
	                destination: new Cesium.Cartesian3.fromDegrees(self.initPosition[0], self.initPosition[1], 10000000)
	            })
	    	console.log(self.initPosition[0], self.initPosition[1], self.initPosition[2]);
	    })
	});
    
}




//双击放大屏幕，并飞到点击位置       


n2.rightBtn.prototype.double_click = function(){

	var self = this; 
    var viewer = self.viewer = n2.viewer;
	var scene = viewer.scene;
	var camera = viewer.camera;
	var handler= new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
	
  	document.getElementById("cesiumContainer").addEventListener('doubletap', function(event) {
//		console.log(event.detail.center)
  		var startX = event.detail.center.x;
  		var startY = event.detail.center.y;
  		var cartesian2 = new Cesium.Cartesian2(startX,startY);
  		
  		var cartesian = scene.globe.pick(viewer.camera.getPickRay(cartesian2), scene);

  		var cartographic = scene.globe.ellipsoid.cartesianToCartographic(cartesian);
  		var point=[ cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180];   //经纬度
  				
		//var q = viewer.scene.pickPosition(cartesian2);  //将点击屏幕的windows Position转化为 cartesian3 position
		
		//当前镜头高度
		
        var height1 = camera.position;
        var height2 = Cesium.Cartographic.fromCartesian(height1).height;
        

        
        //每次放大的距离    设置为当前的一半
        var scale = 5*height2/10;
        
        
        //限制镜头最低位置不然过低会崩溃
        if(scale<2281225){
        	scale = 2281225;
        }
        

        camera.flyTo({ 
            destination:Cesium.Cartesian3.fromDegrees(point[0],point[1],scale),
            duration:1
        })

	}, false);

	//双手指放大缩小事件
	//双手指放大缩小球，左下方的南海诸岛出现和隐藏
	/*
    handler.setInputAction(function (event){  
    	
    	
    	
    	var ig = document.querySelector('#map');
    	//获取当前视角的高度                                        
        var height1 = camera.position;
        var height2 = Cesium.Cartographic.fromCartesian(height1).height;
        
        
        
        if(height2<=2899687){
        	
//      	ig.style.display = 'none';
        	
        }else if(2899687<height2 && height2<=4587498){
        	
//      	ig.style.display = 'block';
        	
        }else if(height2<=1009792){   //视角低于这个高度就开始加载湖北wms
        	
        	
        	
        }else{
        	
//      	ig.style.display = 'none';
        }
           
    	
// 	},Cesium.ScreenSpaceEventType.PINCH_MOVE);
	},Cesium.ScreenSpaceEventType.WHEEL);
*/
}
